package com.bitBusy.manageyourriches.utilityClasses.adapters;

import java.util.HashMap;
import java.util.Map;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bitBusy.manageyourriches.R;

/**
 * Adapter class for display of buttons in a grid
 */
public class ImageButtonAdapterGridView extends BaseAdapter {

	/** Current View context*/
	private Context context;

	/** Map of category names and picID*/
	private Map<String, Integer> myCategoryImageMap;
	
	/** Array of keys*/
	private Object[] myKeyArray;
	
	/** Parameterized constructor
	 * @param the_context the application context
	 * @param the_categoryList list of categories
	 */
    public ImageButtonAdapterGridView(Context the_context, Map<String, Integer> the_categoryImgMap) {
        if (the_categoryImgMap != null)
        {
            context = the_context;
        	myCategoryImageMap = the_categoryImgMap;
        	myKeyArray =  the_categoryImgMap.keySet().toArray();
       // 	myPrevChoice = the_prevChoice==null?"":the_prevChoice;
        }
    }

    public int getCount() {
    	if(myCategoryImageMap != null)
    	{
    		return myCategoryImageMap.size();
    	}
    	return 0;
    }

    public Map<String, Integer> getItem(int position) {
    	
    	if (myKeyArray != null && position < myKeyArray.length)
    	{
    		Map<String, Integer> returnMap = new HashMap<String, Integer>(); 
    		returnMap.put(myKeyArray[position].toString(), myCategoryImageMap.get(myKeyArray[position].toString()) );
    		return returnMap;
    	}
    	return null;
    }
    
   
    public long getItemId(int position) {
        return 0;
    }

    /** create a new ImageView for each item referenced by the Adapter
     *@param position of the object in the list
     *@param view object
     *@param parent view group
     */
    public View getView(int position, View convertView, ViewGroup parent) {
    	LayoutInflater inflater = (LayoutInflater) context
    			.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
     
    		View gridView;
     
    		if (convertView == null) {
     
    			gridView = new View(context);
    			
     
    			// get layout from menu_button_home.xml
    			gridView = inflater.inflate(R.layout.menu_button_home, null);
    		}
    		else {
    			gridView = (View) convertView;
    		}
    			TextView textView = (TextView) gridView
    					.findViewById(R.id.grid_item_text);
    			ImageView imageView = (ImageView) gridView
    					.findViewById(R.id.grid_item_image);
     
    			if (myCategoryImageMap != null && myKeyArray != null && position < myKeyArray.length)
    			{
    		
    				textView.setText(myKeyArray[position].toString());    			
    				imageView.setImageResource(myCategoryImageMap.get(myKeyArray[position]));
    				/*if (myKeyArray[position].toString().equals(myPrevChoice))
    				{
    					gridView.setBackgroundColor(color.lightestGray);
    				}*/
    			}
    		
     
    		return gridView;
    		}

  
}
